﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace ToDoList.Logic
{
    public class MangeMatter
    {
        public bool AddMatter(int listId,string pastTime,string content)
        {
            int matterId = DataSource.matters.Max(a => a.MatterID) + 1;
            var list = DataSource.detailedLists.SingleOrDefault(a => a.ListID == listId);
            var matter = DataSource.matters.SingleOrDefault(a => a.MatterID == matterId);
            if (list != null && matter == null)
            {
                list.matters = new List<Matter>();
                DataSource.matters.Add(
                     new Matter { MatterID = matterId, Time = DateTime.Now, PastTime = pastTime, Content = content });
                list.matters.Add(
                    new Matter { MatterID = matterId, Time = DateTime.Now, PastTime = pastTime, Content = content });
                list.User.mattes.Add(
                    new Matter { MatterID = matterId, Time = DateTime.Now, PastTime = pastTime, Content = content });
                matter.User.UserID = list.User.UserID;
                return true;
            }
            else
                return false;

        }



        public bool DeleteMatter(int matterId,int userId)
        {
            var user = DataSource.users.SingleOrDefault(s => s.UserID == userId);
            var matter = DataSource.matters.SingleOrDefault(a => a.MatterID == matterId);
            if (user != null && matter != null)
            {
                user.mattes.Remove(matter);
                DataSource.matters.Remove(matter);
                return true;
            }
            else
                return false;

        }


        public bool AddRemark(int matterId, string remark)
        {
            var matter = DataSource.matters.SingleOrDefault(a => a.MatterID == matterId);
            if (matter != null)
            {
                matter.Remark += remark;
                return true;
            }
            else
                return false;

        }

        public bool FinishMatter(int matterId,int userId)
        {
            var matter = DataSource.matters.SingleOrDefault(a => a.MatterID == matterId);
            if (matter != null)
            {
                matter.Status = true;
                DataSource.users.SingleOrDefault(a => a.UserID == userId).mattes.Select(m => m.Status = true);
                return true;
            }
            else
                return false;
        }


        public bool CancelFinishMatter(int matterId, int userId)
        {
            var matter = DataSource.matters.SingleOrDefault(a => a.MatterID == matterId);
            if (matter != null)
            {
                matter.Status = false ;
                DataSource.users.SingleOrDefault(a => a.UserID == userId).mattes.Select(m => m.Status = false);
                return true;
            }
            else
                return false;
        }


        public List<Matter> ViewAllMatter(int userId)
        {
            var user = DataSource.users.SingleOrDefault(m => m.UserID == userId);
            if (user != null)
            {
                return user.mattes;
            }
            else
                return null;

        }

        public List<Matter> ViewUnfinishMatter(int userId)
        {
            var user = DataSource.users.SingleOrDefault(a => a.UserID == userId);
            if (user != null)
            {
                return user.mattes.Where(a => a.Status == false).ToList();
            }
            else
                return null;
        }

        public List<Matter> OverdueMatterChangeStatus()
        {
            var overdueMatter = DataSource.matters.Where(a => DateTime.Parse(a.PastTime) < DateTime.Now);
            foreach (var item in overdueMatter)
                item.IsOverdue = true;
            var unOverdueMatter = DataSource.matters.Where(a => DateTime.Parse(a.PastTime) > DateTime.Now);
            foreach (var item1 in unOverdueMatter)
                item1.IsOverdue = false;
            if (overdueMatter != null)
                return overdueMatter.ToList();
            else
                return null;

         }

    }

}
